<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>诊室管理</title>
    <link href="../layui/css/layui.css" rel="stylesheet" />
    <link href="../css/login.css" rel="stylesheet" />
</head>
<body>
<div>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">科室</label>
                <div class="layui-input-block">
                    <select name="dn" id="dn"  lay-filter="xmFilter">
                        <option value=""></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">诊室名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name"  id="name"  class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select name="status" id="status"  lay-filter="xmFilter">
                        <option value="">请选择</option>
                        <option value="0">启用</option>
                        <option value="1">停用</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
                </div>
            </div>
        </div>
    </form>
    <button id="addnotebook" type="button" class="layui-btn layui-btn-normal">添加</button>
</div>



<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="roleTpl">
    {{# if (d.statusId=== 0) { }}
    启用
    {{# } else if(d.statusId=== 1) { }}
    禁用
    {{# } }}
</script>

    <script src="../js/jquery.min.js"></script>
    <script src="../layui/layui.js"></script>
    <script>
        layui.use(['form', 'upload', 'layer','table'],function(){
            var form = layui.form;
            $.ajax({
                url: 'http://localhost:8090/role/role',
                type: 'post',
                success: function (data) {
                    console.log(data);//下面会提到这个data是什么值
                    //使用循环遍历，给下拉列表赋值
                    $.each(data.data, function (index, value) {
                        //console.log(data.data);
                        $('#dn').append(new Option(value.name));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");//重新渲染 固定写法
                }
            })
        });



        var table = layui.table;

        table.render({
            elem: '#test'
            ,url: 'http://localhost:8090/clinic/showclinic'
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,method:'post'
            ,response:{
                statusName: 'code' ,// 对应 code
                msgName: 'msgg' , // 对应 msg
                dataName: 'data' // 对应 data
            }
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code ==  200 ? 0 : res.code, //解析接口状态
                    "data": res.data //解析数据列表
                };
            }
            ,cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field:'name', width:150, title: '科室'}
                ,{field:'department', width:150, title: '诊室名称'}
                ,{field:'address', width:150, title: '地址'}
                ,{field:'statusId', width:150, title: '状态',sort: true, align:'center',templet:'#roleTpl'}

            ]]
        });



        layui.use('form', function(){
            var form = layui.form;

            //监听提交
            form.on('submit(formDemo)', function(data){
                var table = layui.table;

                table.render({
                    elem: '#test'
                    ,url: 'http://localhost:8090/clinic/showclinic'
                    ,where:{
                        "name":$("#name").val(),
                        "dn":$("#dn").val(),
                        "status":$("#status").val()
                    }
                    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    ,method:'post'
                    ,response:{
                        statusName: 'code' ,// 对应 code
                        msgName: 'msgg' , // 对应 msg
                        dataName: 'data' // 对应 data
                    }
                    ,parseData: function(res){ //res 即为原始返回的数据
                        return {
                            "code": res.code ==  200 ? 0 : res.code, //解析接口状态
                            "data": res.data //解析数据列表
                        };
                    }
                    ,cols: [[
                        {type: 'checkbox', fixed: 'left'},
                        {field:'name', width:150, title: '科室'}
                        ,{field:'department', width:150, title: '诊室名称'}
                        ,{field:'address', width:150, title: '地址'}
                        ,{field:'statusId', width:150, title: '状态',sort: true, align:'center',templet:'#roleTpl'}
                            ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                    ]]
                });
                return false;
            });
        })




        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    $.ajax({
                        url:'http://localhost:8090/clinic/deleteRoom',
                        type:'post',
                        data:data,
                        success:function (param){
                            console.log(param)
                            if(param.data>= 1){
                                alert("删除成功")
                            }
                        }
                    })
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });


        //弹窗
        layui.use([ 'layer', 'table',  "laypage","jquery"], function(){
            var
                layer = layui.layer //弹层
                ,table = layui.table //表格
                ,laypage = layui.laypage //分页
                ,$ = layui.jquery

            $('#addnotebook').on("click",function(){
                layer.open({
                    //调整弹框的大小
                    area:['500px','300px'],
                    shadeClose:true,//点击旁边地方自动关闭
                    //动画
                    anim:2,
                    //弹出层的基本类型
                    type: 2,
                    //刚才定义的弹窗页面
                    content: 'clinicAdd.html', //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']

                });
            });
        });
    </script>

</body>
</html>